package category_brand

import (
	. "awesomeProject/goods-web/api"
	"awesomeProject/goods-web/forms"
	"awesomeProject/goods-web/global"
	"awesomeProject/goods-web/proto"
	"context"
	"strconv"

	"github.com/gin-gonic/gin"
	"net/http"
)

func GetCategoryBrandList(ctx *gin.Context) {

	idStr := ctx.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 32)
	if err != nil {
		HandleGrpcErrorToHttp(err, ctx)
		return
	}

	rsp, err := global.GoodsSrvClient.GetCategoryBrandList(context.Background(), &proto.CategoryInfoRequest{
		Id: int32(id),
	})
	if err != nil {
		HandleGrpcErrorToHttp(err, ctx)
		return
	}

	rspMap := make([]interface{}, 0)

	for _, data := range rsp.Data {
		brandInfo := make(map[string]interface{})
		brandInfo["name"] = data.Name
		brandInfo["id"] = data.Id
		brandInfo["logo"] = data.Logo

		rspMap = append(rspMap, brandInfo)
	}
	ctx.JSON(http.StatusOK, rspMap)
}

func CategoryBrandList(ctx *gin.Context) {
	// 把所有分类拿出来，获得所以分类的id，接着再通过GetCategoryBrandList，获取到一个分类对应的品牌

	//所有的list返回的数据结构
	/*
		{
			"total": 100,
			"data":[{},{}]
		}
	*/
	pn := ctx.DefaultQuery("pn", "0")
	pnInt, _ := strconv.Atoi(pn)
	pSize := ctx.DefaultQuery("psize", "10")
	pSizeInt, _ := strconv.Atoi(pSize)
	rsp, err := global.GoodsSrvClient.CategoryBrandList(context.Background(), &proto.CategoryBrandFilterRequest{
		Pages:       int32(pnInt),
		PagePerNums: int32(pSizeInt),
	})
	if err != nil {
		HandleGrpcErrorToHttp(err, ctx)
		return
	}

	rspMap := map[string]interface{}{
		"total": rsp.Total,
	}

	result := make([]interface{}, 0)
	for _, value := range rsp.Data {

		categoryBrandInfo := make(map[string]interface{})

		categoryBrandInfo["id"] = value.Id
		categoryBrandInfo["category"] = map[string]interface{}{
			"id":   value.Category.Id,
			"name": value.Category.Name,
		}
		categoryBrandInfo["brand"] = map[string]interface{}{
			"id":   value.Brand.Id,
			"name": value.Brand.Name,
			"logo": value.Brand.Logo,
		}

		result = append(result, categoryBrandInfo)
	}

	rspMap["data"] = result
	ctx.JSON(http.StatusOK, rspMap)

}

func NewCategoryBrand(ctx *gin.Context) {

	categoryBrandForm := forms.CategoryBrandForm{}
	if err := ctx.ShouldBindJSON(&categoryBrandForm); err != nil {
		HandleValidateError(err, ctx)
		return
	}

	rsp, err := global.GoodsSrvClient.CreateCategoryBrand(context.Background(), &proto.CategoryBrandRequest{
		BrandId:    int32(categoryBrandForm.BrandId),
		CategoryId: int32(categoryBrandForm.CategoryId),
	})
	if err != nil {
		HandleGrpcErrorToHttp(err, ctx)
		return
	}

	rspMap := make(map[string]interface{})
	rspMap["id"] = rsp.Id

	ctx.JSON(http.StatusOK, rspMap)
}

func DeleteCategoryBrand(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 32)
	if err != nil {
		HandleGrpcErrorToHttp(err, ctx)
		return
	}

	_, err = global.GoodsSrvClient.DeleteCategoryBrand(context.Background(), &proto.CategoryBrandRequest{
		Id: int32(id),
	})
	if err != nil {
		HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": "删除成功",
	})

}

func UpdateCategoryBrand(ctx *gin.Context) {
	categoryBrandForm := forms.CategoryBrandForm{}
	if err := ctx.ShouldBindJSON(&categoryBrandForm); err != nil {
		HandleValidateError(err, ctx)
		return
	}

	idStr := ctx.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 32)
	if err != nil {
		HandleGrpcErrorToHttp(err, ctx)
		return
	}

	_, err = global.GoodsSrvClient.UpdateCategoryBrand(context.Background(), &proto.CategoryBrandRequest{
		Id:         int32(id),
		CategoryId: int32(categoryBrandForm.CategoryId),
		BrandId:    int32(categoryBrandForm.BrandId),
	})
	if err != nil {
		HandleGrpcErrorToHttp(err, ctx)
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"msg": "更新成功",
	})
}
